<template>
  <div class="home">
    <div class="header">
      <h1>欢迎来到 德莱联盟</h1>
      <div class="user-actions">
        <template v-if="!userStore.isLoggedIn">
          <el-button type="primary" @click="goToLogin">登录</el-button>
          <el-button @click="goToRegister">注册</el-button>
        </template>
        <template v-else>
          <span>欢迎, {{ userStore.userInfo.username || '用户' }}!</span>
          <el-dropdown>
            <el-button>
              个人中心<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToProfile">个人资料</el-dropdown-item>
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </div>
    </div>

    <div class="search-box">
      <el-input
        v-model="searchKeyword"
        placeholder="搜索音乐、歌手、专辑"
        size="large"
        @keyup.enter="performSearch"
      >
        <template #append>
          <el-button icon="Search" @click="performSearch" />
        </template>
      </el-input>
    </div>

    <div class="navigation">
      <el-menu mode="horizontal" :default-active="activeMenu" @select="handleMenuSelect">
        <el-menu-item index="/">首页</el-menu-item>
        <el-menu-item index="/songs">歌曲列表</el-menu-item>
        <el-menu-item index="/singers">歌手列表</el-menu-item>
        <el-menu-item index="/albums">专辑列表</el-menu-item>
        <el-menu-item index="/ranks">排行榜</el-menu-item>
      </el-menu>
    </div>

    <div class="content">
      <router-view />
    </div>
  </div>
</template>

<script>
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/store/user'
import { ElMessage } from 'element-plus'
import { onMounted, ref, watch } from 'vue'

export default {
  name: 'Home',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const userStore = useUserStore()
    const activeMenu = ref('/')
    const searchKeyword = ref('')

    const goToLogin = () => {
      router.push('/login')
    }

    const goToRegister = () => {
      router.push('/register')
    }

    const handleLogout = () => {
      userStore.logout()
      ElMessage.success('已退出登录')
    }

    const handleMenuSelect = (index) => {
      router.push(index)
    }

    const goToSongs = () => {
      router.push('/songs')
    }

    const goToSingers = () => {
      router.push('/singers')
    }

    const goToAlbums = () => {
      router.push('/albums')
    }

    const goToRanks = () => {
      router.push('/ranks')
    }

    const goToProfile = () => {
      router.push('/profile')
    }

    const goToSongLists = () => {
      router.push('/songlists')
    }

    const goToMySongLists = () => {
      router.push('/songlists')
    }

    const goToHistory = () => {
      router.push('/history')
    }

    const goToRecent = () => {
      router.push('/recent')
    }

    const performSearch = () => {
      if (searchKeyword.value.trim()) {
        router.push(`/search?keyword=${encodeURIComponent(searchKeyword.value)}`)
      }
    }

    // 页面加载时如果已登录则获取用户信息
    onMounted(async () => {
      if (userStore.isLoggedIn) {
        try {
          await userStore.fetchUserInfo()
        } catch (error) {
          console.error('获取用户信息失败:', error)
        }
      }
    })

    // 监听路由变化，更新激活的菜单项
    watch(
      () => route.path,
      (newPath) => {
        if (newPath.startsWith('/singer/')) {
          activeMenu.value = '/singers'
        } else if (newPath.startsWith('/album/')) {
          activeMenu.value = '/albums'
        } else if (newPath.startsWith('/rank/')) {
          activeMenu.value = '/ranks'
        } else if (newPath.startsWith('/songlist/')) {
          activeMenu.value = '/songlists'
        } else {
          activeMenu.value = newPath
        }
      },
      { immediate: true }
    )

    return {
      userStore,
      activeMenu,
      searchKeyword,
      goToLogin,
      goToRegister,
      handleLogout,
      handleMenuSelect,
      goToSongs,
      goToSingers,
      goToAlbums,
      goToRanks,
      goToProfile,
      goToSongLists,
      goToMySongLists,
      goToHistory,
      goToRecent,
      performSearch
    }
  }
}
</script>

<style scoped>
.home {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header h1 {
  color: #333;
  margin: 0;
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-actions span {
  margin-right: 10px;
}

.search-box {
  max-width: 600px;
  margin: 0 auto 30px;
}

.navigation {
  margin-bottom: 30px;
  margin-left: 35%;
}

.content {
  text-align: center;
}

.content p {
  color: #666;
  font-size: 18px;
  margin-bottom: 30px;
}

.features {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

.feature-card {
  width: 250px;
  cursor: pointer;
}

.feature-card:hover {
  transform: translateY(-5px);
  transition: transform 0.3s ease;
}

.card-header {
  font-weight: bold;
  font-size: 16px;
}

.card-content {
  color: #666;
  font-size: 14px;
}
</style>
